package com.didispace;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;

@Controller
public class TurbineContoller {
	
	@RequestMapping("/myhystrix")
	public String home(Model model, WebRequest request) {
		model.addAttribute("basePath", extractPath(request));
		return "myindex";
	}
	@RequestMapping("/myhystrix/{path}")
	public String monitor(@PathVariable String path, Model model, WebRequest request) {
		model.addAttribute("basePath", extractPath(request));
		model.addAttribute("contextPath", request.getContextPath());
		return  path;
	}
	private String extractPath(WebRequest request) {
		String path = request.getContextPath()
				+ request.getAttribute("org.springframework."
						+ "web.servlet.HandlerMapping.pathWithinHandlerMapping",
						RequestAttributes.SCOPE_REQUEST);
		return path;
	}
}
